package com.wiz.web.controllor;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.wiz.entity.Roles;
import com.wiz.entity.User;
import com.wiz.entity.UserRoles;
import com.wiz.service.RolesService;
import com.wiz.service.UserService;
import com.wiz.util.PageView;
import com.wiz.vo.ResponseJsonBody;

/**
 * 用户
 * @author xxy
 *
 */
@Controller
@RequestMapping("/user")
public class UserController {
	private transient final Logger log=LoggerFactory.getLogger(getClass());
	private Gson gson = new GsonBuilder().disableHtmlEscaping().create();
	@Autowired
	private UserService userService;
	@Autowired
	private RolesService rolesService;
	/**
	 * @param model
	 * 存放返回界面的model
	 * @return
	 */
	@RequestMapping("query")
	public String query(Model model, User user, String pageNow) {
		PageView pageView = null;
		if (StringUtils.isEmpty(pageNow)) {
			pageView = new PageView(1);
		} else {
			pageView = new PageView(Integer.parseInt(pageNow));
		}
		pageView = userService.query(pageView, user);
		model.addAttribute("pageView", pageView);
		return "/user/list";
	}

	/**
	 * 跑到新增界面
	 * 
	 * @param model
	 * @return
	 */
	@RequestMapping("addUI")
	public String addUI() {
		return "/user/add";
	}
	/**
	 * 保存数据
	 * 
	 * @param model
	 * @param videoType
	 * @return
	 */
	@RequestMapping("add")
	@ResponseBody
	public String add(HttpServletRequest request,HttpServletResponse response,
			@RequestParam(value = "name", required = true) String name,//登录名
			@RequestParam(value = "password", required = true) String password,//密码
			@RequestParam(value = "nickname", required = true) String nickname,//昵称
			@RequestParam(value = "phone", required = true) String phone,//电话
			@RequestParam(value = "mail", required = true) String mail,//邮箱
			@RequestParam(value = "parentNumber", required = true) String parentNumber,//上级代理用户名编号
			@RequestParam(value = "status", required = true) String status,//用户状态
			@RequestParam(value = "mark", required = true) String mark,//备注
			HttpSession session, Model model){
		ResponseJsonBody json = null;
		User user = new User(0, name, password, nickname, phone, mail,
				 parentNumber, mark, status);
		try {
			userService.add(user);
 			json = new ResponseJsonBody("200", "保存成功", "sys_user",
					"navTab", "closeCurrent", null, null);
		} catch (Exception e) {
			log.error(e.getMessage());
			e.printStackTrace();
			json = new ResponseJsonBody("300", e.getMessage(), "sys_user",
					"navTab", "closeCurrent", null, null);
		}
		return gson.toJson(json);
	}


	/**
	 * 删除
	 * 
	 * @param model
	 * @param videoTypeId
	 * @return
	 */
	@RequestMapping("deleteById")
	@ResponseBody
	public String deleteById(Model model, String userId) {
		ResponseJsonBody json = null;
		try {
			userService.delete(userId);
			json = new ResponseJsonBody("200", "修改成功", "sys_user",
					"", "", "", "");
		} catch (Exception e) {
			log.error(e.getMessage());
			e.printStackTrace();
			json = new ResponseJsonBody("300", e.getMessage(), "sys_user",
					"", "", "", "");
		}
		return gson.toJson(json);
	}
	

	/**
	 * 修改界面
	 * 
	 * @param model
	 * @param videoTypeIds
	 * @return
	 */
	@RequestMapping("getById")
	public String getById(Model model, String userId, int type) {
		User user = userService.getById(userId);
		model.addAttribute("user", user);
		 List<Roles> roles=rolesService.findAll();
		 model.addAttribute("roles", roles);
		if (type == 1) {
			return "/user/edit";
		} else {
			return "/user/show";
		}
	}
	/**
	 * 更新类型
	 * 
	 * @param model
	 * @return
	 */
	@RequestMapping("update")
	@ResponseBody
	public String update(HttpServletRequest request,HttpServletResponse response,
			@RequestParam(value = "id", required = true) String id,//登录名
			@RequestParam(value = "name", required = true) String name,//登录名
			@RequestParam(value = "password", required = true) String password,//密码
			@RequestParam(value = "nickname", required = true) String nickname,//昵称
			@RequestParam(value = "phone", required = true) String phone,//电话
			@RequestParam(value = "mail", required = true) String mail,//邮箱
			@RequestParam(value = "parentNumber", required = true) String parentNumber,//上级代理用户名编号
			@RequestParam(value = "status", required = true) String status,//用户状态
			@RequestParam(value = "mark", required = true) String mark,//备注
			@RequestParam(value = "roleId", required = true) String roleId,//备注
			HttpSession session, Model model){
		ResponseJsonBody json = null;
		User user = new User(Integer.valueOf(id), name, password, nickname, phone, mail,
				 parentNumber, mark, status);
		try {
			userService.modify(user);
			if(roleId!=null){
				UserRoles userRoles = new UserRoles(Integer.valueOf(roleId), Integer.valueOf(id));
				rolesService.saveUserRole(userRoles);
			}
			json = new ResponseJsonBody("200", "修改成功", "sys_user",
					"navTab", "closeCurrent", null, null);
		} catch (Exception e) {
			log.error(e.getMessage());
			e.printStackTrace();
			json = new ResponseJsonBody("300", e.getMessage(), "sys_user",
					"navTab", "closeCurrent", null, null);
		}
		return gson.toJson(json);
		
	}
	
	
	/**
	 * 给用户分配角色界面
	 * @return
	 */
	@RequestMapping("userRole")
	public String userRole(Model model,String userId){
		User user = userService.getById(userId);
		model.addAttribute("user", user);
		List<Roles> roles = rolesService.findAll();
		model.addAttribute("roles", roles);
		return "/user/userRole";
	}
	/**
	 * 保存用户分配角色
	 * @return
	 */
	@RequestMapping("allocation")
	@ResponseBody
	public String allocation(HttpServletRequest request,HttpServletResponse response,
			@RequestParam(value = "userId", required = true) String userId,//登录名
			@RequestParam(value = "roleId", required = true) String roleId,//
			HttpSession session, Model model){
		ResponseJsonBody json = null;
		try {
			if(userId == null || userId.equals("")){
				new Exception("登录名不能为空");
			}
			if(roleId == null || roleId.equals("")){
				new Exception("角色不能为空");
			}
			UserRoles userRoles = new UserRoles(Integer.valueOf(roleId), Integer.valueOf(userId));
			rolesService.saveUserRole(userRoles);
			json = new ResponseJsonBody("200", "修改成功", "sys_user",
					"navTab", "closeCurrent", null, null);
		} catch (Exception e) {
			log.error(e.getMessage());
			e.printStackTrace();
			json = new ResponseJsonBody("300", e.getMessage(), "sys_user",
					"navTab", "closeCurrent", null, null);
		}
		return gson.toJson(json);
	}
}